package mp

import (
	"context"
	"net/http"

	"ruikc.com/server/api/internal/svc"
	"ruikc.com/server/api/internal/types"
	"ruikc.com/server/utils"

	"github.com/zeromicro/go-zero/core/logx"
)

type FileUploadLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewFileUploadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FileUploadLogic {
	return &FileUploadLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *FileUploadLogic) FileUpload(r *http.Request) (resp *types.FileResp, err error) {
	result, err := utils.UploadFile(r, "file", l.svcCtx.Config.Storage.Folder)
	if err != nil {
		return nil, err
	}
	return &types.FileResp{
		Name: result["oldName"],
		Url:  l.svcCtx.Config.Storage.Url + "/" + result["fileName"],
	}, nil

}
